﻿using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Threading.Tasks;
using NFinal;

//此代码由NFinalCompiler生成。
//http://bbs.nfinal.com
namespace NFinalCompiler.Sql
{
    /// <summary>
    /// 模板类
    /// </summary>
    [View("/NFinalCompiler/Sql/ModelTemplate.cshtml")]
    public class ModelTemplate : NFinal.View.RazorView<NFinalCompiler.Sql.Data.TableData>
    {
        /// <summary>
        /// 模板类初始化函数
        /// </summary>
        /// <param name="writer">写对象</param>
        /// <param name="Model">数据</param>
        public ModelTemplate(NFinal.IO.Writer writer, NFinalCompiler.Sql.Data.TableData Model) : base(writer, Model)
        {
        }
        /// <summary>
        /// 输出模板内容
        /// </summary>
        public override void Execute()
        {
            this.ExecuteAsync().Wait();
        }
        /// <summary>
        /// 输出模板内容(异步)
        /// </summary>
        public override async Task ExecuteAsync()
        {
            await writer.WriteAsync("");
            await writer.WriteAsync("using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\n\r\nnamespace ");
            await writer.WriteAsync(NFinalCompiler.Sql.SqlDocument.nameSpace);
            await writer.WriteAsync("\r\n{\r\n    /// <summary>\r\n    /// ");
            await writer.WriteAsync(Model.Name);
            await writer.WriteAsync("\r\n    ///</summary>\r\n");
            if (Model.TableAttribute != null)
            {
                string.Format(Model.TableAttribute, Model.Name);
            }
            await writer.WriteAsync("    public class ");
            await writer.WriteAsync(Model.Name);
            await writer.WriteAsync("\r\n    {\r\n");
            foreach (NFinalCompiler.Sql.Data.ColumnData column in Model.ColumnDataList)
            {
                if (string.IsNullOrEmpty(column.Description))
                {
                    await writer.WriteAsync("            ");
                    await writer.WriteAsync("/// <summary>\r\n            ");
                    await writer.WriteAsync("/// ");
                    await writer.WriteAsync(column.Name);
                    await writer.WriteAsync("\r\n            ");
                    await writer.WriteAsync("/// </summary>\r\n");
                }
                else
                {
                    await writer.WriteAsync("            ");
                    await writer.WriteAsync("/// <summary>\r\n            ");
                    await writer.WriteAsync("/// ");
                    await writer.WriteAsync(column.Description);
                    await writer.WriteAsync("\r\n            ");
                    await writer.WriteAsync("///</summary>\r\n");
                }
                if (column.IsKey)
                {
                    await writer.WriteAsync("            ");
                    if (Model.KeyAttribute != null) { string.Format(Model.KeyAttribute, column.Name); }
                    await writer.WriteAsync("\r\n");
                }
                if (!string.IsNullOrEmpty(Model.ColumnAttribute))
                {
                    await writer.WriteAsync("            ");
                    if (Model.KeyAttribute != null) { string.Format(Model.ColumnAttribute, column.Name); }
                    await writer.WriteAsync("\r\n");
                }
                await writer.WriteAsync("        ");
                await writer.WriteAsync("public ");
                await writer.WriteAsync(column.TypeString);
                await writer.WriteAsync(" ");
                await writer.WriteAsync(column.Name);
                await writer.WriteAsync(" { get; set; }\r\n");
            }
            await writer.WriteAsync("    }\r\n}");
        }
    }
}
